package models.data;

import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * Created with IntelliJ IDEA.
 * User: bjornth
 * Date: 5.3.2013
 * Time: 10:29
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Notendur extends Model {

    @Id
    @Constraints.Required
    public String notandi_id;
    @Constraints.Required
    public String skoli_id;
    @Constraints.Required
    public String notandanr;
    @Constraints.Required
    public String kennitala;

    public Integer nemi;

    public String nafn;



    public static Finder<String,Notendur> find = new Finder<String,Notendur>(
            String.class, Notendur.class
    );

    ///if notandi_id is student, this function will return true
    public static boolean checkIfStudent(String notandiId) {
        Notendur notandi = Notendur.find.byId(notandiId);
        if(notandi.nemi == null){
            return false;
        }
        if (notandi.nemi == 1)
            return true;
        else
            return false;
    }

    public static String findUser(String kennitala) {
        Notendur notandi = Notendur.find.byId(kennitala);
        String nafn = notandi.nafn;
        return nafn;
    }

    public static String findNameFromNotandiId(String notandi_id) {
        Notendur notandi = Notendur.find.byId(notandi_id);
        String nafn = notandi.nafn;
        return nafn;
    }

    public static String getSkolaID(String notendanr) {
        Notendur notandi = find.byId(notendanr);
        String skolaId = notandi.skoli_id;
        return skolaId;
    }


    public static Notendur getNotandiFromKennitala (String kennitala) {
        return find.where().eq("kennitala", kennitala).findUnique();
    }

    public static Notendur authenticate(String kennitala, String notandanr) {
        return find.where().eq("kennitala", kennitala).eq("notandanr", notandanr).findUnique();
    }

}
